<template>
  <section>
    <el-row>
      <el-col :span="24">
        <!--表单-->
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="课程名称">
            <el-input v-model="formInline.course.name" placeholder="课程名称" style="width: 140px;"></el-input>
          </el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="handleEdit">新增</el-button>
        </el-form>
        <!--表格-->
        <el-table
          :data="tableData"
          height="500"
          border
          style="width: 100%">
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form label-position="left" inline class="demo-table-expand">
                <el-form-item label="编号">
                  <span>{{ props.row.id }}</span>
                </el-form-item>
                <el-form-item label="课程名称">
                  <span>{{ props.row.name }}</span>
                </el-form-item>
                <el-form-item label="单节课时费">
                  <span>{{ props.row.singleClassTimeFee }}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column type="selection">
          </el-table-column>
          <el-table-column
            prop="id"
            label="编号"
            width="90">
          </el-table-column>
          <el-table-column
            prop="name"
            label="课程名称"
            width="90">
          </el-table-column>
          <el-table-column
            prop="singleClassTimeFee"
            label="单节课时费"
            width="90">
          </el-table-column>
          <el-table-column label="操作" width="180">
            <template scope="scope">
              <el-button type="primary" class="el-icon-edit" size="small" @click="handleEdit(scope.$index, scope.row)">
                编辑
              </el-button>
              <el-button type="danger" class="el-icon-delete" size="small"
                         @click="handleDelete(scope.$index, scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-size="pageSize"
            :page-sizes="[2, 4, 6, 8]"
            layout="total,sizes,prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </div>
      </el-col>
    </el-row>
    <el-dialog :visible.sync="dialogFormVisible" title="修改课程" :visible="dialogFormVisible" size="tiny"
               :before-close="handleClose">
      <el-form ref="course" :model="course" label-width="80px">
        <el-form-item label="课程名称">
          <el-input v-model="course.name"></el-input>
        </el-form-item>
        <el-form-item label="单节课时费">
          <el-input v-model="course.singleClassTimeFee"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSave" :loading="editLoading">保存</el-button>
          <el-button @click="dialogFormVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </section>
</template>

<script>
  const ERR_OK = "success";
  export default {
    data() {
      return {
        formInline: {
          course: {
            name: '',
            shortName: ''
          }
        },
        tableData: [],
        options: [],
        places: [],
        dialogFormVisible: false,
        editLoading: false,
        course: {
          id: '',
          name: '',
          singleClassTimeFee: ''
        },
        remainClassCount: [{
          courseName: '',
          remainClassCount: ''
        }],
        currentPage: 1,
        pageSize: 2,
        total: 10,
        table_index: 999,
      };
    },
    created() {
      this.handleQuery();
    },
    methods: {
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {
          });
      },
      handleQuery() {
        console.log("query invoke！！！");
        var param = {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          name: this.formInline.course.name,
        };
        this.$http.get('/course', {params: param}).then((response) => {
          response = response.data;
          if (response.status === ERR_OK) {
            this.tableData = response.data.courseInfos;
            this.total = response.data.total;
          } else {
            this.$message.error('操作失败');
          }
        });
      },
      handleDelete(index, row) {
        this.course = Object.assign({}, row);
        this.$confirm('确认删除吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          cancelButtonClass: 'cancel'
        }).then(() => {
          var param = {id: this.course.id}
          this.$http.delete('/course', {params: param}).then((response) => {
            response = response.data;
            if (response.status === ERR_OK) {
              this.$message({
                message: "操作成功！",
                type: 'success'
              });
              this.handleQuery();
            } else {
              this.$message.error('操作失败');
            }
          });
        }).catch(() => {
        });
      },
      handleEdit(index, row) {
        this.dialogFormVisible = true;
        this.course = Object.assign({}, row);
        this.table_index = index;
      },
      handleSave() {
        this.$confirm('确认修改吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          cancelButtonClass: 'cancel'
        }).then(() => {
          this.editLoading = true;
          this.tableData.splice(this.table_index, 1, this.course);
          if (this.course.id != null) {
            this.$http.patch('/course', this.course).then((response) => {
              response = response.data;
              if (response.status === ERR_OK) {
                this.$message({
                  message: "操作成功！",
                  type: 'success'
                });
                this.handleQuery();
              } else {
                this.$message.error('操作失败');
              }
            });
          } else {
            this.$http.post('/course', this.course).then((response) => {
              response = response.data;
              if (response.status === ERR_OK) {
                this.$message({
                  message: "操作成功！",
                  type: 'success'
                });
                this.handleQuery();
              } else {
                this.$message.error('操作失败');
              }
            });
          }
          this.editLoading = false;
          this.dialogFormVisible = false;
        }).catch(() => {
          this.$message({
            message: "操作失败！",
            type: 'fail'
          });
        });
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pageSize = val;
        this.handleQuery();
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.currentPage = val;
        this.handleQuery();
      }
    }
  };
</script>

<style scoped>

  .demo-table-expand {
    font-size: 0;
  }

  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }

  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }

  .el-pagination {
    text-align: center;
    margin-top: 30px;
  }

  .el-message-box__btns .cancel {
    float: right;
    margin-left: 10px;
  }
</style>
